<?php

	/* dependence */
	CoreProject::Using('core.log');

	/**
	 * @package		CoreProject
	 * @subpackage	Db
	 *
	 * cpr_Db_aSql
	 * cpr_Db_SqlConfig
	 * 
	 */

	abstract class cpr_Db_aSql
	{
		const CPR_FETCH_TYPE_ARRAY		= 0;
		const CPR_FETCH_TYPE_OBJECT		= 1;

		/**
		 * @var	resource
		 */
		protected $_connect;
		
		/**
		 * @var	resource
		 */
		protected $_result;
		
		/**
		 * @var	int
		 */
		private $_errorCode = 0;
		
		/**
		 * @var	string
		 */
		private $_errorDesc;
		
		/**
		 * @var	cpr_Db_SqlConfig
		 */
		protected $_sqlConfig;
		
		/**
		 * @param	cpr_Db_SqlConfig	$sqlConfig
		 */
		public function __construct($_sqlConfig)
		{
			$this->_sqlConfig = $_sqlConfig;
		}
		
		/**
		 * @return	string
		 */
		public function GetErrorString()
		{
			return $this->_errorDesc;
		}
		
		/**
		 * @return	int
		 */
		public function GetErrorCode()
		{
			return $this->_errorCode;
		}

		/**
		 * @return	string
		 */
		public function GetError()
		{
			return 'MySQL: '.$this->GetErrorString().' ('.$this->GetErrorCode().')';
		}
		
		/**
		 * @param	string	$_errorDesc
		 * @param	int		$_errorCode
		 */
		public function SetError($_errorDesc, $_errorCode = 0)
		{
			cpr_Core_Log::Write('DB: < '.$_errorDesc);
			$this->_errorDesc = $_errorDesc;
			$this->_errorCode = (int) $_errorCode;
		}
		
	}
	
	class cpr_Db_SqlConfig
	{
		/**
		 * @var	string
		 */
		private $_host;
		
		/**
		 * @var	string
		 */
		private $_login;
		
		/**
		 * @var	string
		 */
		private $_password;
		
		/**
		 * @var	string
		 */
		private $_databaseName;
		
		/**
		 * @param	string	$_host
		 * @param	string	$_login
		 * @param	string	$_password
		 * @param	string	$_dbname = null
		 * @return	cpr_Db_SqlConfig
		 */
		public function cpr_Db_SqlConfig($_host, $_login, $_password, $_dbname = null)
		{
			$this->_host = $_host;
			$this->_login = $_login;
			$this->_password = $_password;
			$this->_databaseName = $_dbname;
		}
		
		/**
		 * @return	string
		 */
		public function Host()
		{
			return $this->_host;
		}
		
		/**
		 * @return	string
		 */
		public function Login()
		{
			return $this->_login;
		}

		/**
		 * @return	string
		 */
		public function Password()
		{
			return $this->_password;
		}
		
		/**
		 * @return	string
		 */
		public function DataBaseName()
		{
			return (null === $this->_databaseName) ? '' : $this->_databaseName;
		}
	}
